package com.trnnn.strutslit;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ActionKey implements Comparable<ActionKey> {

	private String namespace;
	private String actionName;
	private String pattern;
	private int order = 0;

	private Set<String> paramNameSet = new HashSet<String>();

	private Map<String,Object> paramMap=new HashMap<String,Object>();
	
	private static final Pattern paramPattern = Pattern
			.compile("(?<=\\{)([^\\}]*?)(?=\\})");

	public ActionKey(String namespace, String actionName, String pattern) {
		this(namespace, actionName, pattern, 0);
	}

	public ActionKey(String namespace, String actionName, String pattern,
			int order) {
		this.namespace = namespace;
		this.actionName = actionName;
		this.pattern = pattern;
		this.order = order;
		prepareParamNameSet();
	}

	private void prepareParamNameSet() {
		if (this.pattern == null)
			throw new IllegalArgumentException("this.pattern is null");
		Matcher matcher = paramPattern.matcher(this.pattern);
		while (matcher.find()) {
			String paramName = matcher.group();
			this.paramNameSet.add(paramName);
		}
		
	}
	
	public Set<String> paramNameSet(){
		return this.paramNameSet;
	}

	public String namespace() {
		return namespace;
	}

	public void namespace(String namespace) {
		this.namespace = namespace;
	}

	public String actionName() {
		return actionName;
	}

	public void actionName(String actionName) {
		this.actionName = actionName;
	}

	public String pattern() {
		return pattern;
	}

	public void pattern(String pattern) {
		this.pattern = pattern;
	}

	public int order() {
		return order;
	}

	public void order(int order) {
		this.order = order;
	}

	@Override
	public boolean equals(Object object) {
		if (object == this)
			return true;
		if (!(object instanceof ActionKey))
			return false;
		ActionKey key = (ActionKey) object;
		if (key.namespace.equals(namespace)
				&& key.actionName.equals(actionName)
				&& key.pattern.equals(pattern) && key.order == order) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("[Action] namespace ->> ");
		sb.append(namespace);
		sb.append(" actionName ->> ");
		sb.append(actionName);
		sb.append(" urlPattern ->> ");
		sb.append(pattern);
		sb.append(" order ->> ");
		sb.append(order);
		return sb.toString();
	}

	@Override
	public int hashCode() {
		int h1 = this.namespace.hashCode();
		int h2 = this.actionName.hashCode();
		int h3 = this.pattern.hashCode();
		int h4 = this.order;
		return h1 + h2 + h3 + h4;
	}

	@Override
	public int compareTo(ActionKey key) {
		if (!key.namespace.equals(namespace))
			return key.namespace.compareTo(namespace);
		if (!key.actionName.equals(actionName))
			return key.actionName.compareTo(actionName);
		if (order > key.order)
			return 1;
		else if (order < key.order)
			return -1;
		else
			return 0;
	}

	public static void main(String[] args) {

		String str = "/abd/{id}-{name}/{age}";
		// PatternMatcher matcher=new Perl5Matcher();
		Matcher matcher = paramPattern.matcher(str);
		int c = matcher.groupCount();
		System.out.println(c);
		while (matcher.find()) {
			String s = matcher.group();
			System.out.println(s);
		}
	}
}
